package com.gopi.utilities.action;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import com.gopi.utilities.gui.ImageLoader;

public class MenuItemDescriptor 
{
	private String actionCommand; // action command associate with the menu
	private String displayText; // display text
	private String imageKey; // image key - Imageloader will be used to fetch the menu image null for no image
	private ActionListener menuActionListener; // actin listener for the menu
	private List<MenuItemDescriptor> childNodes; // child nodes of the menu
	private int menuType; // menu type - not used now
	
	//- list of menu types
	public static final int MAIN_MENU = 0;
	public static final int MENU_ITEM = 1;
	//-
	
	public static final MenuItemDescriptor SEPARATOR_MENU = 
		new MenuItemDescriptor("-","-");
	
	public MenuItemDescriptor(String displayText)
	{
		this(displayText,"","",null,null);
	}

	public static List<MenuItemDescriptor> getMenuList(MenuItemDescriptor[] items)
	{
		ArrayList<MenuItemDescriptor> list = new ArrayList<MenuItemDescriptor>();
		for(MenuItemDescriptor item : items)
		{
			list.add(item);
		}
		return list;
	}
	
	public MenuItemDescriptor(String displayText,String actionCommand)
	{
		this(displayText,actionCommand,"",null,null);
	}
	
	public MenuItemDescriptor(String displayText,String actionCommand, List<MenuItemDescriptor> childNodes)
	{
		this(displayText,actionCommand,"",null,childNodes);
	}
	
	public MenuItemDescriptor(String displayText,String actionCommand,String imageKey)
	{
		this(displayText,actionCommand,imageKey,null,null);
	}
	
	public MenuItemDescriptor(String displayText,String actionCommand,String imageKey,
			ActionListener menuActionListener)
	{
		this(displayText,actionCommand,imageKey,menuActionListener,null);
	}
	
	public MenuItemDescriptor(String displayText,String actionCommand,String imageKey,
			ActionListener menuActionListener, List<MenuItemDescriptor> childNodes)
	{
		this(displayText,actionCommand,imageKey,menuActionListener,childNodes,MENU_ITEM);
	}

	public MenuItemDescriptor(String displayText,String actionCommand,String imageKey,
			ActionListener menuActionListener, List<MenuItemDescriptor> childNodes,int menuType)
	{
		this.actionCommand = actionCommand;
		this.displayText = displayText;
		this.imageKey = imageKey;
		this.menuActionListener = menuActionListener;
		this.childNodes = childNodes;
		this.menuType = menuType;
	}
	
	public String getActionCommand() {
		return actionCommand;
	}

	public void setActionCommand(String actionCommand) {
		this.actionCommand = actionCommand;
	}

	public String getDisplayText() {
		return displayText;
	}

	public void setDisplayText(String displayText) {
		this.displayText = displayText;
	}

	public String getImageKey() {
		return imageKey;
	}

	public void setImageKey(String imageKey) {
		this.imageKey = imageKey;
	}

	public ActionListener getMenuActionListener() {
		return menuActionListener;
	}

	public void setMenuActionListener(ActionListener menuActionListener) {
		this.menuActionListener = menuActionListener;
	}
	
	public void addChild(MenuItemDescriptor desc)
	{
		if(childNodes==null)
			childNodes = new LinkedList<MenuItemDescriptor>();
		childNodes.add(desc);
	}
	
	public void setChildNodes(List<MenuItemDescriptor> childNodes)
	{
		childNodes = null;
		this.childNodes = childNodes;
	}
	
	public int getChildCount()
	{
		return childNodes==null ? 0 : childNodes.size();
	}
	
	public boolean isSeparator()
	{
		return "-".equals(getDisplayText());
	}
	
	public MenuItemDescriptor[] getChildren()
	{
		return childNodes!=null ? childNodes.toArray(new MenuItemDescriptor[]{}) : null;
	}
	
	public MenuItemDescriptor getChild(int i)
	{
		return childNodes==null || i<0 || i>=childNodes.size() ? null : childNodes.get(i);
	}
	
	public JMenu buildMenuUsingDescriptor(AbstractController controller)
	{
		return buildMenuRecursively(this,controller);
	}
	
	public JMenu buildMenuRecursively(MenuItemDescriptor desc,AbstractController controller)
	{
		JMenu menu = new JMenu(desc.getDisplayText());
		menu.setActionCommand(desc.getActionCommand());
		int child = desc.getChildCount();
		for(int i = 0;i<child;++i)
		{
			if(desc.getChild(i).isSeparator())
				menu.add(new JSeparator());
			else
			{
				menu.add(buildRecursively(desc.getChild(i),controller));
			}
		}
		return menu;
	}
	
	public JMenuItem buildMenuItemUsingDescriptor(AbstractController controller)
	{
		return buildRecursively(this,controller);
	}
	
	public JMenuItem buildRecursively(MenuItemDescriptor desc,AbstractController controller)
	{
		JMenuItem menuItem = new JMenuItem(desc.getDisplayText());
		if(desc.getImageKey()!=null && desc.getImageKey().length()>0)
		{
			menuItem.setIcon(ImageLoader.getInstance().getIcon(desc.getImageKey()));
		}
		menuItem.setActionCommand(desc.getActionCommand());
		menuItem.addActionListener(desc.getMenuActionListener());
		JMenuItem childItem;
		controller.addActionCommand(menuItem.getActionCommand(),menuItem);
		if(desc.getChildCount()>0)
		{
			int count = desc.getChildCount();
			for(int i = 0;i<count;++i)
			{
				if(desc.isSeparator())
					menuItem.add(new JSeparator());
				else
				{
					childItem = buildRecursively(desc.getChild(i),controller);
					menuItem.add(childItem);
				}
			}
		}
		return menuItem;
	}
}
